package graphEd.saisie;

import graphEd.Infos;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import table.EditorTable;
import DataStorage.CodageCheminDAOHibernate;
import HibernateSQLite.java.model.CodageChemin;

import com.mxgraph.util.mxResources;

/**
 * Classe de saisie des informations de codage (codage du chemin). Permet de
 * donner l'ordre des OP et le chemin correspondant � la ligne.
 */
public class EditorCodage extends JPanel{
	private static final long serialVersionUID = 1L;
	private JButton less = new JButton("-");
	private JButton more = new JButton("+");
	private EditorTable table;
	@SuppressWarnings("unused")
	private List<String> listOp, listChemin;
	private List<Long> oldCodage_id = new ArrayList<Long>();
	private Infos infos = Infos.getInstance();

	/**
	 * Constructeur. Installe aussi les ActionListeners pour les boutons + et -. 
	 */
	public EditorCodage(){
		listOp = new ArrayList<String>();
		listChemin = new ArrayList<String>();
		table = new EditorTable(new String[] { mxResources.get("numberCode"),
				mxResources.get("values"), mxResources.get("op"), mxResources.get("op2")});
		table.setValueAt(table.getColumnCount()-1, 0, table.getColumnCount()-1);
		makeLayout();
		installListeners();
	}
	
	
	
	/**
	 * Méthode appelée pour créer la partie codage d'une nouvelle ligne.
	 */
	public void create(long infoLigneId){
		CodageCheminDAOHibernate codageDAO = new CodageCheminDAOHibernate();
		List<String> listOp = table.getValuesInRow(2,1);
		List<String> listOp2 = table.getValuesInRow(3,1);
		List<String> listChemin = table.getValuesInRow(1,1);

	
		for(Long l:oldCodage_id){
			codageDAO.deleteById(l);
		}		
		
		codageDAO.saveCodageChemin(listOp, listOp2, listChemin, infoLigneId);
		if (oldCodage_id.isEmpty()){
			for(CodageChemin c:codageDAO.getListCodageChemin(infoLigneId)){
				oldCodage_id.add(c.getCodageChemin_id());
			}		
		}
	}
	
	/**
	 * 
	 * @param infoLigneId
	 */
	public void open(long infoLigneId){
		CodageCheminDAOHibernate codageDAO = new CodageCheminDAOHibernate();
		List<CodageChemin> listCodage = codageDAO.getListCodageChemin(infoLigneId);
		String op, op2, chemin;
		
		
		if(!listCodage.isEmpty()){
			for(int i=0; i<listCodage.size(); i++){
				if(i<listCodage.size()-1){
					table.addColumn("test");
				}
				table.setValueAt(i+1, 0, i+1);
				// R�cup�ration des valeurs
				op = listCodage.get(i).getOP();
				op2 = listCodage.get(i).getOP_2();
				chemin = listCodage.get(i).getChemin();
				
				oldCodage_id.add(listCodage.get(i).getCodageChemin_id());
			
				table.setValueAt(chemin, 1, i+1);
				table.setValueAt(op, 2, i+1);
				table.setValueAt(op2, 3, i+1);
			}
			infos.setCodageSize(listCodage.size());
		}
	}
	
	private void makeLayout(){
		this.setLayout(new BorderLayout());
		//((EditorDefaultTableModel) this.table.getModel()).setColumnUneditable(0);
		//((EditorDefaultTableModel)this.table.getModel()).setRowUneditable(0);
		
		JScrollPane scrollPane = new JScrollPane(table.getTable());
		scrollPane.setBorder(BorderFactory.createLineBorder(Color.gray));
		//scrollPane.setBorder(BorderFactory.createEmptyBorder());
		JPanel buttonGroup = new JPanel();
		buttonGroup.add(less);
		buttonGroup.add(more);

		this.add(scrollPane, BorderLayout.CENTER);
		this.add(buttonGroup, BorderLayout.SOUTH);
	}
	
	private void installListeners(){
		more.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent evt) {
				table.addColumn("");
				table.setValueAt(""+(table.getColumnCount()-1), 0, table.getColumnCount()-1);
			}
		});
		less.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent evt) {
				table.removeColumn(table.getTable().getColumnCount()-1);
			}
		});
	}
	
	
	public EditorTable getTable() {
		return table;
	}


	public void setTable(EditorTable table) {
		this.table = table;
	}
}
